/* eslint-disable no-inner-declarations */
/* eslint-disable no-unused-vars */
/* eslint-disable no-useless-return */
import  utils from '@/utils/'
const  { TokenKey,getCookie, HomeIndexPathKey,getToken } = utils


const loginPath = '/login'
// 选择行业路径

const indexPath = process.env.NUXT_APP_INDEX_PATH
// 路由白名单，直接绕过路由守卫
const whiteList = [loginPath,  'register']

/**
 * @description  鉴权中间件，用于校验登陆
 *
 */
import {Nothy} from 'element-ui'
export default async ({ redirect, route, req }) => {
  const { path, fullPath, query } = route
  const {  redirectPath } = query

  // 应对刷新 vuex状态丢失的解决方案

//   if (process.server) {
    const cookie =  req&&req.headers.cookie
    const token = getToken()

    const homeIndexPath = HomeIndexPathKey
    const homePath = homeIndexPath || indexPath
    console.log("*/",req,getCookie,cookie,token,TokenKey)
    if (token) {
      const isAllLoginPath = path === loginPath
      if (path === '/') {
        return 
      } else if (isAllLoginPath ) {
        // 已经登录过的，进来是是登录页 并且已经选择过industryCode
        return redirect(homePath)
      } else {
        // 其他的正常跳转
        return
      }
    } else {
   
      // 鉴权白名单
      if (whiteList.includes(path)) return

      // 未登录，进来的不是是登录页，全部重定向到登录页
      if (!path.includes(loginPath)) {
        
        redirect(`${loginPath}?redirect=${encodeURIComponent(fullPath)}`)
      }else{
        console.log("   return",123)
        // return
      }
    }
//   }
}
